package com.blog4j.article.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.blog4j.article.service.ApplyArticleService;
import com.blog4j.article.vo.req.ApplyArticleListReqVo;
import com.blog4j.article.vo.req.ApproveArticleReqVo;
import com.blog4j.article.vo.resp.ApplyArticleRespVo;
import com.blog4j.common.annotation.OperationLog;
import com.blog4j.common.enums.ModuleTypeEnum;
import com.blog4j.common.enums.OperationTypeEnum;
import com.blog4j.common.model.Result;
import com.blog4j.requestRate.annotation.RequestRate;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/7/24 12:15
 **/
@RestController
@RequestMapping("/apply-article")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ApplyArticleController {
    private final ApplyArticleService applyArticleService;

    /**
     * 分页查询申请文章信息列表
     *
     * @param reqVo 请求
     * @return 文章信息列表
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.ARTICLE, description = "分页查询申请文章信息列表")
    @SaCheckLogin
    @PostMapping("/list")
    public Result list(@RequestBody @Valid ApplyArticleListReqVo reqVo) {
        PageInfo<ApplyArticleRespVo> pageInfo = applyArticleService.applyArticleList(reqVo);
        return Result.ok(pageInfo);
    }

    /**
     * 审批文章
     *
     * @param reqVo 审批信息
     * @return 审批成功
     */
    @RequestRate(count = 5)
    @OperationLog(operationType= OperationTypeEnum.EDIT,
            module= ModuleTypeEnum.ARTICLE,
            description = "审批文章")
    @SaCheckRole(value = {"SUPER_ADMIN", "ORGANIZATION_ADMIN"}, mode = SaMode.OR)
    @PostMapping("/approve")
    public Result approve(@RequestBody @Valid ApproveArticleReqVo reqVo) {
        applyArticleService.approve(reqVo);
        return Result.ok();
    }
}
